package com.lyy.common;

import com.google.common.base.Predicates;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;


/**
 * swagger2自动装配
 * @author liushiwei
 */
@EnableConfigurationProperties(value = LyySwagger2Properties.class)
@Slf4j
@Configuration
public class LyySwagger2AutoConfiguration {

    @Autowired
    private LyySwagger2Properties properties;

    @Bean
    public Docket createRestApi() {
        log.info("--------加载Swagger2配置---------");

        return new Docket(DocumentationType.SWAGGER_2)
                // 接口请求地址
                .host(properties.getHost())
                .apiInfo(apiInfo())
                // 设置正式环境不显示Swagger2
                .enable(properties.getShow())
                .select()
                .apis(
                        // 配置多个扫描路径","分割的方式
                        Predicates.or(
                                RequestHandlerSelectors.basePackage(properties.getBasePackage())
                        )
                )
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        // name：作者，url：通常项目地址，email：邮箱
        Contact contact=new Contact(properties.getName(),properties.getUrl(),properties.getEmail());
        return new ApiInfoBuilder()
                //标题
                .title(properties.getTitle())
                // 描述
                .description(properties.getDescription())
                .contact(contact)
                //版本
                .version(properties.getVersion())
                .build();
    }
}
